package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * Created by david on 2014-06-10.
 */
public class ObjectListView extends ListView {
    private int cornerRadius = 10;
    private int[] colors = new int[]{Color.parseColor("#BFEEFF"), Color.parseColor("#40B9FF")};
    private GradientDrawable.Orientation orientation = GradientDrawable.Orientation.TOP_BOTTOM;

    private GradientDrawable shape,round,top,bottom;

    public ObjectListView(Context context) {
        super(context);
        initDrawable();
        GradientDrawable drawable = createSelectorDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[]{ Color.parseColor("#FFFFFF"),Color.parseColor("#CCCCCC")},new float[]{10,10,10,10,10,10,10,10});
        drawable.setStroke(1, Color.parseColor("#808080"));
        drawable.setColor(Color.WHITE);
        setBackground(drawable);
    }

    public ObjectListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObjectListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public int[] getColors() {
        return colors;
    }

    public void setColors(int[] colors) {
        this.colors = colors;
        initDrawable();
    }

    public GradientDrawable.Orientation getOrientation() {
        return orientation;
    }

    public void setOrientation(GradientDrawable.Orientation orientation) {
        this.orientation = orientation;
        initDrawable();
    }

    public int getCornerRadius() {
        return cornerRadius;
    }

    public void setCornerRadius(int cornerRadius) {
        if (this.cornerRadius != cornerRadius) {
            this.cornerRadius = cornerRadius;
            initDrawable();
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                int itemnum = pointToPosition(x, y);

                if (itemnum == AdapterView.INVALID_POSITION) {
                    break;
                } else{
                    if(itemnum==0){
                        if(itemnum==(getAdapter().getCount()-1)){
                            if (round == null) {
                                initDrawable();
                            }
                            setSelector(round);//R.drawable.app_list_corner_round
                            Log.d("ObjectLV", "Selector:app_list_corner_round");
                        }else{
                            if (top == null) {
                                initDrawable();
                            }
                            setSelector(top);//R.drawable.app_list_corner_round_top
                            Log.d("ObjectLV", "Selector:app_list_corner_round_top");
                        }
                    }else if(itemnum==(getAdapter().getCount()-1)) {
                        if (bottom == null) {
                            initDrawable();
                        }
                        setSelector(bottom);//R.drawable.app_list_corner_round_bottom
                        Log.d("ObjectLV", "Selector:app_list_corner_round_bottom");
                    }else{
                        if (shape == null) {
                            initDrawable();
                        }
                        setSelector(shape);//R.drawable.app_list_corner_shape
                        Log.d("ObjectLV", "Selector:app_list_corner_shape");
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return super.onInterceptTouchEvent(ev);
    }

    private void initDrawable(){
        shape = createSelectorDrawable(orientation, colors, null);
        round = createSelectorDrawable(orientation, colors, new float[]{cornerRadius,cornerRadius,cornerRadius,
                cornerRadius,cornerRadius,cornerRadius,cornerRadius,cornerRadius});
        top = createSelectorDrawable(orientation, colors, new float[]{cornerRadius,cornerRadius,cornerRadius,
                cornerRadius,0,0,0,0});
        bottom = createSelectorDrawable(orientation, colors, new float[]{0, 0, 0, 0, cornerRadius, cornerRadius,
                cornerRadius, cornerRadius});
    }

    /**
     *
     * @return
     */
    public static GradientDrawable createSelectorDrawable(GradientDrawable.Orientation orientation, int[] colors, float[] radii){
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setOrientation(orientation);
        gradientDrawable.setColors(colors);
        if (radii!=null) {
            gradientDrawable.setCornerRadii(radii);
        }
        return gradientDrawable;
    }
}
